package com.simplemall.micro.serv.common.bean.basic;

import java.io.Serializable;

public class Page implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 331587801274856560L;

	public static final int DEFAULT_PAGESIZE = 20;
	
	// -- 分页查询参数 --//
	// 当前页数
	protected int page = 1;

	// 每页显示记录数
	protected int size = DEFAULT_PAGESIZE;

	// 总记录数
	protected int total = 0;
	
	// -- 构造函数 --//
	public Page() {
		size = DEFAULT_PAGESIZE;
	}

	public Page(int size) {
		this.size = size;
	}
	
	public Page(int page, int size) {
		this.size = size;
		this.page   = page;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;

		if (page < 1) {
			this.page = 1;
		}
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}
	
	/**
	 * 根据size与total计算总页数, 默认值为-1.
	 */
	public long getTotalPages() {
		if (total == 0) {
			return 0;
		}

		long count = total / size;
		if (total % size > 0) {
			count++;
		}
		return count;
	}
	
	/**
	 * 是否还有下一页.
	 */
	public boolean isHasNextPage() {
		return (page + 1 <= getTotalPages());
	}

	/**
	 * 取得下页的页号, 序号从1开始. 当前页为尾页时仍返回尾页序号.
	 */
	public int getNextPage() {
		if (isHasNextPage()) {
			return page + 1;
		} else {
			return page;
		}
	}

	/**
	 * 是否第一页.
	 */
	public boolean isFirstPage() {
		return page == 1;
	}

	/**
	 * 是否还有上一页.
	 */
	public boolean isHasPrePage() {
		return (page - 1 >= 1);
	}

	/**
	 * 取得上页的页号, 序号从1开始. 当前页为首页时返回首页序号.
	 */
	public int getPrePage() {
		if (isHasPrePage()) {
			return page - 1;
		} else {
			return page;
		}
	}

	@Override
	public String toString() {
		return "Page [page=" + page + ", size=" + size + ", total="
				+ total + "]";
	}
		
}
